package org.jeecg.modules.wflow.workflow.controller;

import org.jeecg.modules.wflow.utils.R;
import org.jeecg.modules.wflow.workflow.bean.vo.ProcessHandlerParamsVo;
import org.jeecg.modules.wflow.workflow.service.ProcessTaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * @author : willian fu
 * @date : 2022/8/25
 */
@RestController
@RequestMapping("wflow/process/task")
public class ProcessInstanceTaskController {

    @Autowired
    private ProcessTaskService taskService;

    /**
     * 查询用户待办待处理的任务
     *
     * @param pageSize 每页条数
     * @param pageNo   页码
     * @param code     流程定义key，模型ID
     * @return 分页列表数据
     */
    @GetMapping("todoList")
    public Object getUserTodoList(@RequestParam(defaultValue = "20") Integer pageSize,
                                  @RequestParam(defaultValue = "1") Integer pageNo,
                                  @RequestParam(required = false) String code) {
        return R.ok(taskService.getUserTodoList(pageSize, pageNo, code));
    }

    /**
     * 查询用户已审批的流程实例
     *
     * @param pageSize 每页条数
     * @param pageNo   页码
     * @param code     流程定义key，模型ID
     * @return 分页列表数据
     */
    @GetMapping("idoList")
    public Object getUserIdoList(@RequestParam(defaultValue = "20") Integer pageSize,
                                 @RequestParam(defaultValue = "1") Integer pageNo,
                                 @RequestParam(required = false) String code) {
        return R.ok(taskService.getUserIdoList(pageSize, pageNo, code));
    }

    /**
     * 用户处理任务，审批、转交、评论、撤销等操作
     * @param params 操作参数
     * @return 操作结果
     */
    @PostMapping("handler")
    public Object approvalTask(@RequestBody ProcessHandlerParamsVo params) {
        taskService.approvalTask(params);
        return R.ok("处理成功");
    }

    /**
     * 获取所有可回退的审批任务节点
     *
     * @param instanceId 审批实例ID
     * @param taskId     当前任务ID
     * @return 所有可回退节点
     */
    @GetMapping("recall/nodes")
    public Object getRecallTaskNodes(@RequestParam String instanceId,
                                     @RequestParam String taskId) {
        return R.ok(taskService.getRecallTaskNodes(instanceId, taskId));
    }
}
